﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Iacym.Entities;
using Iacym.BusinessLogic;
using Microsoft.Practices.Unity;

namespace Iacym.UI
{
    public partial class FrmTipoReunion : Form
    {
        [Dependency]
        public ITipoReunionBL tipoReuBL { get; set; }

        [Dependency]
        public IUsuarioMenuBL usuMenuBL { get; set; }

        public FrmTipoReunion()
        {
            InitializeComponent();
            this.BuildUp();
        }

        public USUARIO currentUsuarioTIpoReunion { get; set; }

        private void TxtCriterio_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (Char.IsLetter(e.KeyChar))
            {
                e.Handled = false;
            }
            else if (Char.IsControl(e.KeyChar))
            {
                e.Handled = false;
            }
            else if (Char.IsSeparator(e.KeyChar))
            {
                e.Handled = false;
            }
            //else if (char.IsNumber(e.KeyChar))
            //{
            //    e.Handled = false;
            //}
            else
            {
                e.Handled = true;
            }
        }

        private void TxtCriterio_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                tIPO_REUNIONBindingSource.DataSource = tipoReuBL.GetFromTipoReunionByCriterio(TxtCriterio.Text);
            }
        }

        private void BtnBuscar_Click(object sender, EventArgs e)
        {
            tIPO_REUNIONBindingSource.DataSource = tipoReuBL.GetFromTipoReunionByCriterio(TxtCriterio.Text);
        }

        private void FrmTipoReunion_Load(object sender, EventArgs e)
        {
            if (currentUsuarioTIpoReunion != null)
            {
                menuLlena();
            }
            else
                CargarDatos();
        }

        public void menuLlena()
        {
            var datoUsuario = usuMenuBL.GetAllFromUsuarioByPersonaFromLogin(currentUsuarioTIpoReunion.PERSONA.Dni);

            var permisos = (from p in usuMenuBL.GetAllUsuarioMenu(datoUsuario.UsuarioId)
                            select p.MENU.Tag.ToString()).ToArray();

            System.Windows.Forms.Button[] botones = new System.Windows.Forms.Button[] { 
            this.btnEliminar,this.BtnEditar,this.BtnCrear};

            var noVisible = from nv in botones
                            where !permisos.Contains(nv.Tag.ToString())
                            select nv;

            noVisible.ToList().ForEach(nv => nv.Enabled = false);

            CargarDatos();
        }

        private void CargarDatos()
        {
            tIPO_REUNIONBindingSource.DataSource = tipoReuBL.GetAllFromTipoReunion();
        }

        private void BtnCrear_Click(object sender, EventArgs e)
        {
            FrmMttoTipoReunion frm = new FrmMttoTipoReunion();
            frm.Nuevo();
            frm.ShowDialog();
            CargarDatos();
        }

        private void BtnEditar_Click(object sender, EventArgs e)
        {
            var tipoReu = tIPO_REUNIONBindingSource.Current as TIPO_REUNION;

            if (tipoReu != null)
            {
                FrmMttoTipoReunion frm = new FrmMttoTipoReunion();
                frm._currentTipoR = tipoReu;
                frm.Editar();
                frm.ShowDialog();
                CargarDatos();
            }
            else
            {
                MessageBox.Show("Debe seleccionar un ítem.", "Mensaje", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private void btnEliminar_Click(object sender, EventArgs e)
        {
            var tipoReu = tIPO_REUNIONBindingSource.Current as TIPO_REUNION;
            if (tipoReu != null)
            {
                DialogResult rpta = MessageBox.Show("¿Desea eliminar la Empresa: " + tipoReu.Descripcion + "?", "Mensaje", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                if (rpta == DialogResult.Yes)
                {
                    tipoReuBL.RemoveTipoReunion(tipoReu.Id);
                    CargarDatos();
                }
            }
            else
            {
                MessageBox.Show("Debe seleccionar un ítem.", "Mensaje", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }
    }
}
